home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk11 / memocal / calendar.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  13KB  |  465 lines

  1. /***************************************************************/
  2. /*            Calendar1 Program                   */
  3. /*    This program opens a window, reads the system date and   */
  4. /*    displays the current month calendar and date.  The mouse */
  5. /*    may be used to change the date.  Saves/Uses date on Exit */
  6. /*    if requested.                           */
  7. /***************************************************************/
  8.  
  9. /* Modified 30-MAY-88 by Ron Charlton to correctly set initial date. */ 
  10.  
  11. #include <exec/types.h>
  12. #include <exec/devices.h>
  13. #include <exec/io.h>
  14. #include <exec/libraries.h>
  15. #include <devices/timer.h>
  16. #include <functions.h>
  17. #include <intuition/intuition.h>
  18. #include <graphics/gfxmacros.h>
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <time.h>
  22.  
  23. #define INTUITION_REV 33L
  24. #define GRAPHICS_REV  33L
  25. #define MAXLIN          80
  26.  
  27. char *mn[12] = {"January","February","March","April","May","June","July",
  28.         "August","September","October","November","December"};
  29. short nday[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  30. char *Curr_Year = "1984";
  31. char *Time = "12:00";
  32. char s[]="Date 12:00 07-Jul-86";
  33. short CC,CR,da,wk,mo,yr,hr,min,sec;
  34. USHORT pot[2];
  35. short matrix[6][7];
  36. static char *lines[6] = {"              ",  /* init lines for calendar*/
  37.              "              ",
  38.              "              ",
  39.              "              ",
  40.              "              ",
  41.              "              ",
  42.             };
  43.  
  44. struct IntuitionBase  *IntuitionBase;
  45. struct GfxBase          *GfxBase;
  46. struct Window          *Win;
  47. struct FileHandle     *Open();
  48. struct Gadget          Cg[8];
  49. struct PropInfo          Cinfo[2];
  50. struct Image          Cimage[2];
  51.  
  52. main(argc,argv)
  53. int  argc;
  54. char *argv[];
  55. {
  56.     struct IntuiMessage *message;
  57.     struct Gadget    *gad;  
  58.     ULONG  class,code, x,y;
  59.     BOOL   Quit,Save,Use,GadFlag;
  60.     USHORT ID;
  61.     float  real;
  62.     
  63.     short dx,dy,wkdy,maxday;
  64.     VOID  show_month(),Last_Time(),show_day(),FrameIt(),Point_Block();
  65.     VOID  Reset_Date(),Show_Time(),Show_Help(),OpenAll(),Make_Prop_Gadget(),
  66.       Make_BOOL_Gadget(),Get_Hours(),Get_Minutes(),Tag(),
  67.       Set_Time();
  68.     short day_of_week(),set_days();
  69.     long  Convert();
  70.  
  71.     Last_Time();OpenAll();FrameIt(Win);
  72.     maxday=set_days(mo+1,yr);wkdy=day_of_week(mo,yr);
  73.     show_month(da,mo,yr,maxday,wkdy);
  74.     Show_Time(0);Point_Block(CC,CR);
  75.  
  76.     for (Quit=FALSE,Save=FALSE,Use=FALSE,GadFlag=FALSE;!Quit;) {
  77.     while (!(message=GetMsg(Win->UserPort))) WaitPort(Win->UserPort);
  78.     class = message->Class;    code = message->Code;
  79.     x     = message->MouseX;   y    = message->MouseY;
  80.     gad   = (struct Gadget *)message->IAddress;
  81.     ReplyMsg( message );
  82.     switch ( class ) {
  83.         case CLOSEWINDOW: Quit=TRUE;break;
  84.         case GADGETDOWN:  ID=gad->GadgetID;GadFlag=TRUE;
  85.                   if (ID >= 6) /* Save or Use selected */
  86.                   {
  87.                   Quit = TRUE;
  88.                   if ( ID == 6 ) Use = TRUE;
  89.                   if ( ID == 7 ) Save = Use = TRUE;
  90.                   }
  91.                   else if (ID>1) {
  92.                       switch(ID) {
  93.                     case 2: yr--;break;
  94.                     case 3: yr++;break;
  95.                     case 4: mo--;if (mo<0) { yr--;mo=11; }
  96.                         if (da > set_days(mo+1,yr))
  97.                           da=set_days(mo+1,yr);
  98.                         break;
  99.                     case 5: mo++;if (mo>11) { yr++;mo=0; }
  100.                         if (da > set_days(mo+1,yr))
  101.                           da=set_days(mo+1,yr);
  102.                         break;
  103.                        }
  104.                       maxday=set_days(mo+1,yr);
  105.                       wkdy=day_of_week(mo,yr);
  106.                       Point_Block(CC,CR);
  107.                       show_month(da,mo,yr,maxday,wkdy);
  108.                       Point_Block(CC,CR);
  109.                     }
  110.                    break;
  111.         case MOUSEBUTTONS: if (code==SELECTDOWN && y>47 && y<121) {
  112.             dx=(x-6)/24;dy=(y-50)/10;
  113.             if (dx<0) dx=0;if (dx>6) dx=6;
  114.             if (dy<0) dy=0;if (dy>5) dy=5;
  115.             if (matrix[dy][dx]) {
  116.             Point_Block(CC,CR);Point_Block(dx,dy);
  117.             CC=dx;CR=dy;da=matrix[dy][dx];
  118.               }
  119.             GadFlag=FALSE;
  120.           }
  121.         break;
  122.         case MOUSEMOVE:if (ID<2 && GadFlag && Cinfo[ID].HorizPot!=pot[ID]){
  123.             real=(Cinfo[ID].HorizPot+1.0)/65536.0;
  124.             if (ID==0) hr=real*24; else min=real*60;
  125.             pot[ID]=Cinfo[ID].HorizPot;Show_Time();
  126.          }
  127.       }
  128.       }
  129.     CloseWindow(Win);
  130.     if ( yr > 1978 && Use ) {
  131.     Reset_Date();Set_Time(Convert());
  132.     if ( Save ) Tag();
  133.       }
  134. }
  135.  
  136.  
  137. VOID Tag()        /* Write File with current date to S: */
  138. {
  139.     struct FileHandle    *out;
  140.  
  141.     if ((out=Open("S:now",1006L)) != NULL) {
  142.     Write(out,&s,(long)strlen(s));Close(out);
  143.       } else printf("Can't update S:now !!!\n");
  144. }
  145.  
  146.  
  147. VOID OpenAll()        /* Open everything */
  148. {
  149.     struct NewWindow nw;
  150.     USHORT i;
  151.     float  real;
  152.  
  153.     if (!(IntuitionBase=(struct IntuitionBase *)
  154.     OpenLibrary("intuition.library",INTUITION_REV))) {
  155.         printf("Intuition Open failed.\n");
  156.         exit(FALSE);
  157.       }
  158.     if (!(GfxBase=(struct GfxBase *)
  159.     OpenLibrary("graphics.library",GRAPHICS_REV))) {
  160.         printf ("graphics open failed.\n");
  161.         exit(FALSE);
  162.       }
  163.     nw.LeftEdge=232;        nw.TopEdge=12;        nw.Width=178;
  164.     nw.Height=144;        nw.DetailPen=0x00;    nw.BlockPen=0x01;
  165.     nw.Title=(UBYTE *)" Calendar ";
  166.     nw.Flags=ACTIVATE|SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|
  167.         WINDOWDEPTH|REPORTMOUSE;
  168.     nw.IDCMPFlags=CLOSEWINDOW|MOUSEBUTTONS|MOUSEMOVE|GADGETDOWN|GADGETUP;
  169.     nw.Type=WBENCHSCREEN;    nw.FirstGadget=&Cg[0];    nw.CheckMark=NULL;
  170.     nw.Screen=NULL;        nw.BitMap=NULL;     nw.MinWidth=100;
  171.     nw.MinHeight=60;        nw.MaxWidth=640;    nw.MaxHeight=200;
  172.  
  173.     real=(float)hr/24.0;pot[0]=real*65536+1;
  174.     real=(float)min/60.0;pot[1]=real*65536+1;
  175.  
  176.     Make_Prop_Gadget(&Cg[0],8,120,80,7,&Cinfo[0],&Cimage[0],pot[0]);
  177.     Cg[0].NextGadget=&Cg[1];
  178.     Make_Prop_Gadget(&Cg[1],90,120,80,7,&Cinfo[1],&Cimage[1],pot[1]);
  179.     Cg[1].NextGadget=&Cg[2];
  180.     Make_BOOL_Gadget(&Cg[2],9,14,48,8);Cg[2].NextGadget=&Cg[3];
  181.     Make_BOOL_Gadget(&Cg[3],120,14,48,8);Cg[3].NextGadget=&Cg[4];
  182.     Make_BOOL_Gadget(&Cg[4],9,24,48,8);Cg[4].NextGadget=&Cg[5];
  183.     Make_BOOL_Gadget(&Cg[5],120,24,48,8);Cg[5].NextGadget=&Cg[6];
  184.     Make_BOOL_Gadget(&Cg[6],9,130,76,8);Cg[6].NextGadget=&Cg[7];
  185.     Make_BOOL_Gadget(&Cg[7],93,130,76,8);
  186.     for (i=0;i<8;i++) Cg[i].GadgetID=i;
  187.     Win=(struct Window *)(OpenWindow(&nw));
  188. }
  189.  
  190. VOID Make_Prop_Gadget(g,l,t,w,h,info,image,pot)
  191. struct Gadget *g;
  192. USHORT l,t,w,h;
  193. USHORT pot;
  194. struct Image    *image;
  195. struct PropInfo *info;
  196. {
  197.     info->Flags=AUTOKNOB|FREEHORIZ;
  198.     info->VertPot=0L;        info->HorizPot=(ULONG)pot;
  199.     info->HorizBody=1L;        info->VertBody=1L;
  200.     g->NextGadget=NULL;        g->LeftEdge=l;           g->TopEdge=t;
  201.     g->Width=w;            g->Height=h;
  202.     g->Flags=GADGHCOMP|SELECTED;
  203.     g->Activation=GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY;
  204.     g->GadgetType=PROPGADGET;    g->GadgetRender=(APTR)image;
  205.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  206.     g->SpecialInfo=(APTR)info;    g->GadgetID=NULL;    g->UserData=NULL;
  207. }
  208.  
  209. VOID Make_BOOL_Gadget(g,l,t,w,h)
  210. struct Gadget *g;
  211. USHORT    l,t,w,h;
  212. {
  213.     g->NextGadget=NULL;        g->LeftEdge=l;           g->TopEdge=t;
  214.     g->Width=w;            g->Height=h;
  215.     g->Flags=GADGHCOMP;
  216.     g->Activation=GADGIMMEDIATE;
  217.     g->GadgetType=BOOLGADGET;    g->GadgetRender=NULL;
  218.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  219.     g->SpecialInfo=NULL;    g->GadgetID=NULL;    g->UserData=NULL;
  220. }
  221.  
  222. void Reset_Date()        /* Reset Date String to Current Date */
  223. {
  224.     hr %=24;
  225.     s[5]=(hr/10)+48;
  226.     s[6]=(hr%10)+48;
  227.     s[8]=(min/10)+48;
  228.     s[9]=(min%10)+48;
  229.     s[11]=(da/10)+48;
  230.     s[12]=(da%10)+48;
  231.     s[14]=mn[mo][0];
  232.     s[15]=mn[mo][1];
  233.     s[16]=mn[mo][2];
  234.     s[18]=((yr%100)/10)+48;
  235.     s[19]=(yr%10)+48;
  236. }
  237.  
  238. void Show_Time(x)        /* Display Time */
  239. short x;
  240. {
  241.   short h,i,morn;
  242.  
  243.     if (min>59) min=min-60;
  244.     if (min<0) min=60+min;
  245.     if (hr>24) hr=hr-24;
  246.     if (hr<1) hr=24+hr;
  247.     if (hr>12) h=hr-12; else h=hr;
  248.     Time[0]=(h/10)+48;
  249.     Time[1]=(h%10)+48;
  250.     Time[3]=(min/10)+48;
  251.     Time[4]=(min%10)+48;
  252.     Move(Win->RPort,69L,118L);
  253.     Text(Win->RPort,Time,5L);
  254.     Move(Win->RPort,118L,118L);
  255.     SetAPen(Win->RPort,3L); 
  256.     if ((hr>11) && (hr<24)) Text(Win->RPort,"PM",2L);
  257.       else Text(Win->RPort,"AM",2L);
  258.     SetAPen(Win->RPort,1L);   
  259. }
  260.  
  261. void Point_Block(nx,ny)        /* Draw Cursor Square around Day */
  262. SHORT nx,ny;
  263. {
  264.    LONG x,y;
  265.  
  266.     x=nx*24+7;y=ny*10+58;
  267.     SetDrMd(Win->RPort,COMPLEMENT);
  268.     Move(Win->RPort,x+1,y);
  269.     Draw(Win->RPort,x+19,y);
  270.     Draw(Win->RPort,x+19,y-10);
  271.     Draw(Win->RPort,x,y-10);
  272.     Draw(Win->RPort,x,y);
  273.     SetDrMd(Win->RPort,JAM2);
  274. }
  275.  
  276. void FrameIt(w)            /* Draw Frame for Calendar Window */
  277. struct Window *w;
  278. {   
  279.     short i;
  280.  
  281.     SetDrMd(Win->RPort,JAM2);
  282.     SetAPen(Win->RPort,3L);
  283.     Move(w->RPort,5L,12L);
  284.     Draw(w->RPort,172L,12L);
  285.     Draw(w->RPort,172L,141L);
  286.     Draw(w->RPort,5L,141L);
  287.     Draw(w->RPort,5L,12L);
  288.     Move(w->RPort,5L,35L);
  289.     Draw(w->RPort,172L,35L);
  290.     Move(w->RPort,5L,109L);
  291.     Draw(w->RPort,172L,109L);
  292.     Move(w->RPort,5L,128L);
  293.     Draw(w->RPort,172L,128L);
  294.     Move(w->RPort,89L,128L);
  295.     Draw(w->RPort,89L,141L);
  296.     SetAPen(Win->RPort,1L);
  297.     RectFill(Win->RPort,9L,130L,85L,139L);
  298.     RectFill(Win->RPort,93L,130L,168L,139L);
  299.     Move(Win->RPort,22L,118L);Text(Win->RPort,"Time",4L);
  300.     SetAPen(Win->RPort,2L);
  301.     Move(Win->RPort,9L,32L);
  302.     Text(Win->RPort," <<<<        >>>> ",20L);
  303.     Move(Win->RPort,9L,22L);
  304.     Text(Win->RPort," <<<<        >>>> ",20L);
  305.     SetBPen(Win->RPort,1L);
  306.     Move(Win->RPort,15L,137L);Text(Win->RPort,"   Use  ",8L);
  307.     Move(Win->RPort,98L,137L);Text(Win->RPort,"  Save  ",8L);
  308.     SetBPen(Win->RPort,0L);
  309. }
  310.  
  311. void Last_Time()        /* Read the System Date and Time */
  312. {
  313.  
  314.     struct tm *localtime(),*tstruct;
  315.     long   tloc,time(),t;
  316.     short  i;
  317.  
  318.     tloc=time();
  319.     tstruct=localtime(&tloc);
  320.     mo=tstruct->tm_mon;
  321.     yr=tstruct->tm_year+1900;
  322.     da=tstruct->tm_yday;
  323.     wk=tstruct->tm_wday;
  324.     hr=tstruct->tm_hour;
  325.     min=tstruct->tm_min;
  326.     sec=tstruct->tm_sec;
  327.     for (i=0;i<mo;i++) da -= set_days(i+1,yr);
  328.     if (da<1) {
  329.     mo-=1;da=set_days(mo+1,yr);
  330.       } 
  331. }
  332.  
  333. short day_of_week(month,yr)    /* Calculate the Day of the Week */
  334. short month,yr;
  335. {
  336.     float r;
  337.     short i,j,k,l;
  338.  
  339.     r=365.0*yr+1+31.0*month;month=month+1;
  340.     if (month>2) {
  341.         i=0.4*month+2.3;j=yr/4;k=yr/100;l=0.75*(k+1);
  342.         r=r-i+j-l;
  343.     } else {
  344.         i=(yr-1)/4;j=(yr-1)/100;k=0.75*(j+1);
  345.         r=r+i-k;
  346.       }
  347.     r=r-273750.0;
  348.     j=(-1.0*r)/7.0;i=r+j*7.0;
  349.     if(i==7) i=0;
  350.     return i;
  351. }
  352.  
  353. void show_month(da,month,yr,mxday,wkday)  /* Display Day, Month, Year */
  354. short da,month,yr,mxday,wkday;
  355. {
  356.     short cnt,row,i,j,x,y;
  357.  
  358.     for (i=0;i<6;i++) {
  359.       for (j=0;j<7;j++) {
  360.         matrix[i][j]=0;
  361.         lines[i][j*3]=32;
  362.         lines[i][j*3+1]=32;
  363.        }
  364.      }
  365.     Move(Win->RPort,49L,32L);
  366.     Text(Win->RPort,"       ",10L);
  367.     SetAPen(Win->RPort,1L);
  368.     i=(178-strlen(mn[month])*8)/2;
  369.     Curr_Year[0]=(yr/1000)+48;
  370.     Curr_Year[1]=((yr%1000)/100)+48;
  371.     Curr_Year[2]=((yr%100)/10)+48;
  372.     Curr_Year[3]=yr%10+48;
  373.     Move(Win->RPort,73L,22L);
  374.     Text(Win->RPort,Curr_Year,4L);
  375.     Move(Win->RPort,(long)i,32L);
  376.     Text(Win->RPort,mn[month],(long)strlen(mn[month]));
  377.     Move(Win->RPort,9L,46L);
  378.     SetAPen(Win->RPort,3L);
  379.     Text(Win->RPort," S  M    T  W  T  F  S",20L);
  380.     cnt=wkday;row=0;
  381.     for (i=1;i<(mxday+1);i++) {
  382.         if (i==da) {
  383.         CR=row;CC=cnt;
  384.         }
  385.         if (i>9) lines[row][cnt*3]=(i/10)+48;
  386.         else lines[row][cnt*3]=32;
  387.         lines[row][cnt*3+1]=(i%10)+48;
  388.         matrix[row][cnt]=i;    
  389.         cnt++;
  390.         if (cnt==7 && i<mxday) {
  391.         cnt=0;row++;
  392.         }
  393.     }
  394.     SetAPen(Win->RPort,1L);
  395.     for (i=0;i<6;i++) {
  396.         Move(Win->RPort,9L,(long)(i*10+56));
  397.         Text(Win->RPort,lines[i],20L);
  398.     }
  399. }
  400.  
  401. short set_days(month,yr)    /* Check for Leap Year */
  402. short month,yr;
  403. {
  404.    short norm;
  405.  
  406.     if (month==1 || month==3 || month==5 || month==7 || 
  407.                 month==8 || month==10 || month==12)
  408.         return 31;
  409.       else if (month==4 || month==6 || month==9 || month==11)
  410.           return 30;
  411.         else {
  412.         norm = 1;
  413.         if ((yr % 4) == 0) norm = 0;
  414.         if ((yr % 100) == 0) norm = 1;
  415.         if ((yr % 400) == 0) norm = 0;
  416.         if (norm) return 28;
  417.             else return 29;
  418.           }
  419. }
  420.  
  421. VOID  Set_Time(secs)     /* Set System Clock */
  422. long secs;
  423. {
  424.     struct timerequest *time_req;
  425.     struct timerequest *CreateExtIO();
  426.     struct MsgPort     *timer_port;
  427.     long   error;
  428.          
  429.     timer_port = CreatePort( 0L, 0L );
  430.     if ( ! timer_port ) exit( 1L );
  431.     time_req=CreateExtIO(timer_port,(long)sizeof(struct timerequest));
  432.     if (time_req==NULL) exit(1L);
  433.     error =  OpenDevice (TIMERNAME,UNIT_MICROHZ,time_req,0L);
  434.     if( error ) {
  435.     DeleteExtIO(time_req,(long)sizeof(struct timerequest));
  436.     DeletePort( timer_port );
  437.     exit( 2L );
  438.       }
  439.     time_req -> tr_node.io_Message . mn_ReplyPort = timer_port;
  440.     time_req -> tr_node.io_Command = TR_SETSYSTIME;
  441.     time_req -> tr_time.tv_secs    = secs;
  442.     time_req -> tr_time.tv_micro   = 0L;
  443.     DoIO( time_req );
  444.     CloseDevice( time_req );
  445.     DeleteExtIO(time_req,(long)sizeof(struct timerequest)); 
  446.     DeletePort( timer_port );
  447.  }
  448.  
  449. long Convert()        /* Convert Time to Seconds */
  450. {
  451.     long secs,years,days;
  452.     short i;
  453.  
  454.     years=yr-1978;
  455.     if (years<0) return(0L);
  456.     for (i=0,days=0;i<mo;i++) days+=nday[i];
  457.     days+=da-1;
  458.     if (yr>1979) {
  459.     days+=((yr-1980)/4)+1;
  460.     if (((yr-1980)%4)==0 && mo<2) days--;
  461.       }
  462.     secs=(years*365+days)*86400+(long)hr*3600+min*60+sec;
  463.     return(secs);
  464.